package Driver;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class LoginAgendaVirtual {
	
	private WebDriver driver;
	
	public LoginAgendaVirtual(WebDriver driver){
		setDriver(driver);
	}
	
	public String logarError(String user, String password){
		//Acessando o sistema
		getDriver().get("http://agendavirtual.freeiz.com/view/");
		
		WebDriverWait wait = new WebDriverWait(getDriver(), 2000);
		
		WebElement inputUser = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("usuario_login")));
		WebElement inputPassword = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("usuario_senha")));
		WebElement btnSubmit = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("logarUsuario")));
		
		inputUser.sendKeys(user);
		inputPassword.sendKeys(password);
		btnSubmit.click();
		
		Alert alert = wait.until(ExpectedConditions.alertIsPresent());
		
		return alert.getText();
	}
	
	public boolean logarSuccess(String user, String password){
		//Acessando o sistema
		getDriver().get("http://agendavirtual.freeiz.com/view/");
		
		WebDriverWait wait = new WebDriverWait(getDriver(), 2000);
		
		WebElement inputUser = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("usuario_login")));
		WebElement inputPassword = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("usuario_senha")));
		WebElement btnSubmit = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("logarUsuario")));
		
		inputUser.sendKeys(user);
		inputPassword.sendKeys(password);
		btnSubmit.click();
		
		WebElement targetSuccess = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("contatos")));
		
		return targetSuccess.isDisplayed();
	}

	public WebDriver getDriver() {
		return driver;
	}

	public void setDriver(WebDriver driver) {
		this.driver = driver;
	}

}
